﻿#include "StoneDefenceUtils.h"

#include "CharacterZD/Core/RuleCharacterZD.h"

ARuleCharacterZD* StoneDefenceUtils::FindTargetRecently(const TArray<ARuleCharacterZD*>& InCharacters, const FVector& Location)
{
	float Distance = 0;
	ARuleCharacterZD* RecentCharacter = nullptr;	
	for (ARuleCharacterZD* TheCharacter : InCharacters)
	{
		if (!TheCharacter->IsActive())
			continue;
		
		if (!RecentCharacter)
		{
			RecentCharacter = TheCharacter;
			Distance = (Location - TheCharacter->GetActorLocation()).Size();
			continue;
		}

		const float TheDistance = (Location - TheCharacter->GetActorLocation()).Size();
		if (TheDistance < Distance)
		{
			RecentCharacter = TheCharacter;
			Distance = TheDistance;
		}
	}
	
	return RecentCharacter;
}
